package com.hitrust.gs.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.business.GSProviderProcessor;
import com.hitrust.gs.form.GSProviderScanQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;

import framework.core.SrvContext;

public class GSProviderScanQueryExportAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
		GSProviderScanQueryForm tForm = (GSProviderScanQueryForm)aForm;
        //取得页面参数组成Bean对象
        GSProviderBean tGSProviderBean = new GSProviderBean();
        //tGSProviderBean.setGs_provider_id(tForm.getProviderId());
        tGSProviderBean.setGs_provider_name(tForm.getProviderName());
        tGSProviderBean.setGs_reside_country(tForm.getCountryCode());
        tGSProviderBean.setGs_reside_province(tForm.getProvinceCode());
        tGSProviderBean.setGs_reside_city(tForm.getDistrictCode());
        //tGSProviderBean.setGs_corp_property(tForm.getCorpProperty());
        tGSProviderBean.setGs_contact_id(tForm.getContactid());
        tGSProviderBean.setGs_contact_phone(tForm.getContactphone());
        if( GSSystemConfigurator.getInstance().getProjectVersionType().equals("SH")){
            tGSProviderBean.setGs_validity(tForm.getStatus()); 
        }
        tGSProviderBean.setIs_audit(tForm.getIsAudit());
        
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		//ServletOutputStream out = null;
		ServletOutputStream out = null;
		HSSFWorkbook wb = null;
        try{
            //查询供应商
            GSProviderProcessor tGSProviderProcesser = new GSProviderProcessor();
            ResultProcess tRp = tGSProviderProcesser.getProviderByOpt(tGSProviderBean);
            tRp.getAllRecoreds();
            
            
            String xls = "Provider.xls";
            aForm.getResponse().setContentType("application/ms-excel");

    		aForm.getResponse().setHeader("Content-disposition", "attachment; filename=" + xls);
    		out = aForm.getResponse().getOutputStream();
    		wb = this.getExportExcel(tRp.getAllRecoreds(), aForm.getRequest());

    			//bos = new BufferedOutputStream(aForm.getResponse().getOutputStream());

//    			byte[] buff = new byte[2048];
//    			int bytesRead;
//
//    			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    				//bos.write(wb.getBytes());
    			//}
    				wb.write(out);

            
//            tForm.getRequest().setAttribute("provider",tRp);
//            
//            //取得所有国家
//            ArrayList tList = GSBaseManager.getAllCountry();
//            aForm.getRequest().setAttribute("country",tList);
//            //取得所有省
//            tList = GSBaseManager.getAllProvince();
//            aForm.getRequest().setAttribute("province",tList);
//            //取得所有地区
//            tList = GSBaseManager.getAllDistinct();
//            aForm.getRequest().setAttribute("district",tList);
//            //取得公司性质
//            String[][] tStr= GSBaseManager.getAllCorpProperty();
//            aForm.getRequest().setAttribute("corpproperty",tStr);
//            aForm.getRequest().setAttribute("requery","false");
        }catch(GSException e){
            aForm.getRequest().setAttribute("action", "A200201.do");
            throw e;
        }finally {
			try {
				if (bos != null){
					bos.flush();
					bos.close();
				}
				if (bis != null){
					bis.close();
				}
				if (out != null){
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
        return null;
	}
	
	public HSSFWorkbook getExportExcel(Collection list, HttpServletRequest request) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String strDate = sdf.format(new Date());
	
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFCellStyle titleStyle = wb.createCellStyle();
			titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
			titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
			HSSFSheet sheet = wb.createSheet();
			
			int fistColumn = 0;					
			
			HSSFRow titleRow = sheet.createRow((short) fistColumn);
			
			HSSFCell titlecell = titleRow.createCell((short) 0);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("供应商ID");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 1);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("供应商编号");
			titlecell.setCellStyle(createTopTitleStyle(wb));
					
			titlecell = titleRow.createCell((short) 2);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("供应商全称");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 3);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("供应商简称");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 4);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("供应商英文名称");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 5);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("公司性质");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 6);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("经营地址");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 7);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("邮政编码");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 8);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("负责人");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 9);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("负责人联系电话");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 10);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("联系人");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 11);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("联系人电话");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 12);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("所属国家");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 13);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("所属省份");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 14);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("所属城市");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 15);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("主营行业");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 16);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("主营产品");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 17);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("经营范围");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 18);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("信誉等级");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 19);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("是否交易平台供应商");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 20);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("是否有效");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			Iterator iter = list.iterator();
			int row = 1; 
			while(iter.hasNext()) {
				GSProviderBean gsProviderBean = (GSProviderBean)iter.next();
				HSSFRow deptrow = sheet.createRow((short) row);
				
				HSSFCell cell = deptrow.createCell((short) 0 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getId())){
					cell.setCellValue(gsProviderBean.getId());
				}
				
				cell = deptrow.createCell((short) 1 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_provider_id())){
					cell.setCellValue(gsProviderBean.getGs_provider_id());
				}		
				
				cell = deptrow.createCell((short) 2 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_provider_name())){
					cell.setCellValue(gsProviderBean.getGs_provider_name());
				}
				
				cell = deptrow.createCell((short) 3 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_provider_shortname())){
					cell.setCellValue(gsProviderBean.getGs_provider_shortname());
				}
				
				cell = deptrow.createCell((short) 4 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_provider_engname())){
					cell.setCellValue(gsProviderBean.getGs_provider_engname());
				}
				
				cell = deptrow.createCell((short) 5 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_corp_property())){
					cell.setCellValue(gsProviderBean.getGs_corp_property());
				}
				
				cell = deptrow.createCell((short) 6 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_corp_addr())){
					cell.setCellValue(gsProviderBean.getGs_corp_addr());
				}
				
				cell = deptrow.createCell((short) 7 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_corp_post())){
					cell.setCellValue(gsProviderBean.getGs_corp_post());
				}
				
				cell = deptrow.createCell((short) 8 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_principal_id())){
					cell.setCellValue(gsProviderBean.getGs_principal_id());
				}
				
				cell = deptrow.createCell((short) 9 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_principal_phone())){
					cell.setCellValue(gsProviderBean.getGs_principal_phone());
				}
				
				cell = deptrow.createCell((short) 10 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_contact_id())){
					cell.setCellValue(gsProviderBean.getGs_contact_id());
				}
				
				cell = deptrow.createCell((short) 11 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_contact_phone())){
					cell.setCellValue(gsProviderBean.getGs_contact_phone());
				}
				
				cell = deptrow.createCell((short) 12 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_reside_country())){
					cell.setCellValue(GSBaseManager.getCountryName(gsProviderBean.getGs_reside_country()));
				}
				
				cell = deptrow.createCell((short) 13 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_reside_province())){
					cell.setCellValue(GSBaseManager.getProvinceName(gsProviderBean.getGs_reside_province()));
				}
				
				cell = deptrow.createCell((short) 14 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_reside_city())){
					cell.setCellValue(GSBaseManager.getDistrictName(gsProviderBean.getGs_reside_city()));
				}
				
				cell = deptrow.createCell((short) 15 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_work_trade())){
					cell.setCellValue(gsProviderBean.getGs_work_trade());
				}
				
				cell = deptrow.createCell((short) 16 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_work_manufacture())){
					cell.setCellValue(gsProviderBean.getGs_work_manufacture());
				}
				
				cell = deptrow.createCell((short) 17 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_work_scope())){
					cell.setCellValue(gsProviderBean.getGs_work_scope());
				}
				
				cell = deptrow.createCell((short) 18 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(gsProviderBean.getGs_credit_level())){
					cell.setCellValue(gsProviderBean.getGs_credit_level());
				}
				
				cell = deptrow.createCell((short) 19 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(gsProviderBean.getGs_is_tradeflat_provider().equals("1")){
					cell.setCellValue("是");
				}else{
					cell.setCellValue("否");
				}
				
				cell = deptrow.createCell((short) 20 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(gsProviderBean.getGs_validity().equals("1")){
					cell.setCellValue("是");
				}else{
					cell.setCellValue("否");
				}
				row ++;
			}		
		return wb;
	}

	public static HSSFCellStyle createTopTitleStyle(HSSFWorkbook wb){
		HSSFCellStyle cellstyle = wb.createCellStyle();
		cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直   
		cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平   
		return cellstyle;
	}
}
